package com.zsw.recongize.result;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.zsw.recongize.bean.Invoice;


public class JxlWrite {
	
	public static void export(List<Invoice> list, File target){
		Workbook wb = null;
		WritableWorkbook workbook = null;
		try {
			wb = Workbook.getWorkbook(JxlWrite.class.getResourceAsStream("/template.xls"));
			workbook = Workbook.createWorkbook(target, wb);
			WritableSheet sheet = workbook.getSheet(0);
//			sheet.getSettings().setDefaultRowHeight(300);
			int i = 2;
			for(Invoice invoice: list){
//				WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.AUTOMATIC); 
//				WritableCellFormat cellFormat = new WritableCellFormat(font1);  
				WritableCellFormat cellFormat = new WritableCellFormat();  
		        //设置边框;  
//		        cellFormat.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);  
		        //设置自动换行;  
//		        cellFormat.setWrap(true);  
		        //设置文字居中对齐方式;  
//		        cellFormat.setAlignment(Alignment.CENTRE);  
		        //设置垂直居中;  
//		        cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);  
				WritableCellFormat cellFormat1 = new WritableCellFormat();  
				cellFormat1.setWrap(true);
				if(invoice.getExceptionMsg() != null){
					cellFormat.setBackground(Colour.RED);  
					cellFormat1.setBackground(Colour.RED);  
				}
				int j = 0;
				sheet.addCell(new Label(j++, i, invoice.getFileName(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getCode(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getNumber(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getDate(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getCheckCode(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getPassword(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getBuyerName(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getBuyerSocialCode(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getBuyerAddress(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getBuyerBankAccount(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getDetailGoods(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailModel(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailUnit(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailCount(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailUnitPrice(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailTotalPrice(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailTaxRate(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getDetailTax(), cellFormat1));
				sheet.addCell(new Label(j++, i, invoice.getTotal(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getSellerName(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getSellerSocialCode(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getSellerAddress(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.getSellerBankAccount(), cellFormat));
				sheet.addCell(new Label(j++, i, invoice.isReal()?"真":"假", cellFormat));
				sheet.addCell(new Label(j++, i, invoice.isBuyerNameCorrect()?"正确":"错误", cellFormat));
				sheet.addCell(new Label(j++, i, invoice.isBuyerSocialCodeCorrect()?"正确":"错误", cellFormat));
				i++;
			}
			workbook.write();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}finally{
			if(wb != null){
				wb.close();
			}
			if(workbook != null){
				try {
					workbook.close();
				} catch (WriteException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
